package entities;

import entities.profiles.VehicleProfile;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
@Entity
@Table(name = "vehicle")
@NamedQueries({
    @NamedQuery(name = "Vehicle.findAll", query = "SELECT v FROM Vehicle v"),
    @NamedQuery(name = "Vehicle.findByIdVehicle", query = "SELECT v FROM Vehicle v WHERE v.idVehicle = :idVehicle"),
    @NamedQuery(name = "Vehicle.findBySeats", query = "SELECT v FROM Vehicle v WHERE v.seats = :seats"),
    @NamedQuery(name = "Vehicle.findByPlateNumber", query = "SELECT v FROM Vehicle v WHERE v.plateNumber = :plateNumber"),
    @NamedQuery(name = "Vehicle.findByBrand", query = "SELECT v FROM Vehicle v WHERE v.brand = :brand"),
    @NamedQuery(name = "Vehicle.findByYear", query = "SELECT v FROM Vehicle v WHERE v.year = :year")})
public class Vehicle implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idVehicle")
    private Integer idVehicle;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "seats")
    private int seats;
    
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 10)
    @Column(name = "plateNumber")
    private String plateNumber;
    
    @Basic(optional = false)
    @NotNull
    @Column(name = "year")
    private int year;
    
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 70)
    @Column(name = "brand")
    private String brand;
    
    
    @JoinColumn(name = "idVehicleProfile", referencedColumnName = "idVehicleProfile")
    @OneToOne(optional = false)
    private VehicleProfile idVehicleProfile;
    
    @JoinColumn(name = "idUser", referencedColumnName = "idUser")
    @ManyToOne(optional = false)
    private User idUser;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "idVehicle")
    private Collection<Trip> trips;

    public Vehicle() {
    }

    public Vehicle(VehicleProfile vehicleProfile, User idUser, int seats, String plateNumber, String brand, int year) {
        this.idUser = idUser;
        this.seats = seats;
        this.plateNumber = plateNumber;
        this.brand = brand;
        this.year = year;
        
        this.trips = new ArrayList<>();
        
        this.idVehicleProfile = vehicleProfile;
    }

    public Integer getId() {
        return idVehicle;
    }

    public int getSeats() {
        return seats;
    }

    public void setSeats(int seats) {
        this.seats = seats;
    }

    public String getPlateNumber() {
        return plateNumber;
    }

    public void setPlateNumber(String plateNumber) {
        this.plateNumber = plateNumber;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }


    public VehicleProfile getVehicleProfile() {
        return idVehicleProfile;
    }
    
    public User getUser() {
        return idUser;
    }
    
    public void setUser(User user) {
        this.idUser = user;
    }
    
    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }
    
    public Collection<Trip> getVehicleTripParticipations() {
        return trips;
    }

    public void participateInTrip(Trip trip) {
        this.trips.add(trip);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idVehicle != null ? idVehicle.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (object == null) {
            return false;
        }
        if (!(object instanceof Vehicle)) {
            return false;
        }
        Vehicle other = (Vehicle) object;
        return !((this.idVehicle == null && other.idVehicle != null) || (this.idVehicle != null && !this.idVehicle.equals(other.idVehicle)));
    }

    @Override
    public String toString() {
        return "entities.Vehicle[ idVehicle=" + idVehicle + " ]";
    }
}
